package com.timoon.component.panel.jqgrid;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;

import com.googlecode.wicket.jquery.ui.Options;
import com.timoon.app.utils.GlobalRandom;
import com.timoon.app.utils.HttpServletUtil;
import com.timoon.component.behavior.jqgrid.JqGridBehavior;
import com.timoon.component.panel.jqgrid.action.AddButton;
import com.timoon.component.panel.jqgrid.action.DelButton;
import com.timoon.component.panel.jqgrid.action.DetailsButton;
import com.timoon.component.panel.jqgrid.action.ModifierButton;
import com.timoon.component.panel.jqgrid.panel.CreatorPanel;
import com.timoon.component.panel.jqgrid.panel.DefaultSearchPanel;
import com.timoon.component.panel.jqgrid.panel.DetailsPanel;
import com.timoon.component.util.ComponentConstant;

/**
 *<p></p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
public abstract class JqGridPanel extends Panel {
	private static final long serialVersionUID = 6494132179615736841L;
	
	public static String ADD_BUTTON_ENABEL="add";
	public static String DELETE_BUTTON_ENABLE="delete";
	public static String MODIFIER_BUTTON_ENABLE="modifier";
	public static String DETAIL_BUTTON_ENABLE="detail";
	public static String SAVE_BUTTON_ENABLE="save";
	
	private JqGridService service;

	/**
	 * 用户选中一条记录，controller会将选中的记录id发送至此，DetailsButton获取此值后做相应处理
	 */
	private String detailsId;
	/**
	 * 每一个实例对应一个key，向controller注册自己
	 */
	private String key=String.valueOf(GlobalRandom.getRandom());
	
	private Map<String,String> configMap;
	
	private DelButton delBtn;
	
	private ModalWindow win;
	
	private ModalWindow win2;
	
	
	public JqGridPanel(String id,Options options,final JqGridService service,Map<String,String> configMap) {
		super(id);
		// TODO Auto-generated constructor stub
		
		/*6.14添加,默認url*/
		options.set("url", "'"+HttpServletUtil.getBasePath()+"/controller/jqgrid/getGridData'");
		/*end*/
		
		this.service=service;
		
		this.configMap=configMap;
		
		this.registerSelf();
		
		//给table标签随机生成id，grid使用此id
		WebMarkupContainer gridContainer=new WebMarkupContainer("wk_grid");
		this.add(gridContainer);
		gridContainer.add(new AttributeModifier("id","grid-"+key));
		
		WebMarkupContainer gridPager=new WebMarkupContainer("wk_grid_pager");
		add(gridPager);
		gridPager.add(new AttributeModifier("id", "gridPager-"+key));
		this.add(new JqGridBehavior("#grid-"+key,this.coverOptions(options)));
		
		//模态窗口
		win=this.createModalWindow("wk_win");
		win.setOutputMarkupId(true);
		this.add(win);
		
		win2=this.createModalWindow("wk_win2");
		win2.setWindowClosedCallback(new ModalWindow.WindowClosedCallback(){
			@Override
			public void onClose(AjaxRequestTarget target) {
				// TODO Auto-generated method stub
				win.close(target);
			}
		});
		add(win2);
		
		
		Form form=new Form("wk_form");
		this.add(form);
		
		//【删除】按钮
		delBtn=new DelButton("wk_delBtn",JqGridPanel.this);
		form.add(delBtn);
		
		//【新增】
		form.add(new AddButton("wk_addBtn",JqGridPanel.this));
		
		//【查看详细】
		form.add(new DetailsButton("wk_detailsBtn",JqGridPanel.this));
		
		//【修改】
		form.add(new ModifierButton("wk_modifierBtn", JqGridPanel.this));
		
		//搜索面板
		form.add(this.getSearchPanel("wk_search_toolbar",JqGridPanel.this));
		
		
	}
	public boolean isButtonVisible(String buttonName){
		return false;
	}
	/**
	 * 获取【查看明细】界面,可覆盖
	 * 
	 * @param id
	 * @param win
	 * @param service
	 * @return
	 */
	public DetailsPanel getDetailsPanel(String id,JqGridPanel mainPanel,String recordId){
		return new DetailsPanel(id,mainPanel,recordId);
	}
	/**
	 * 获取搜索面板,可覆盖
	 * @param id
	 * @return
	 */
	protected DefaultSearchPanel getSearchPanel(String id,JqGridPanel mainPanel){
		return new DefaultSearchPanel(id,mainPanel);
	}
	/**
	 * 删除操作，controller调用，分发至DelButton
	 */
	public void onDelete(String deleteIds){
		this.delBtn.onDelete(deleteIds);
	}
	private ModalWindow createModalWindow(String id){
		ModalWindow win=new ModalWindow(id);
		return win;
	}
	/**
	 * 获取编辑面板，可被覆盖
	 * @param id
	 * @return
	 */
	public CreatorPanel getCreatorPanel(String id,JqGridPanel mainPanel,String recordId){
		return new CreatorPanel(id,mainPanel,recordId);
	}
	/**
	 * 使用默认值覆盖空参数
	 * */
	private Options coverOptions(Options options){
		Map<String,String> map=new HashMap<String,String>(){
			{
				this.put("url", "'"+HttpServletUtil.getBasePath()+"/controller/jqgrid/getGridData'");
				this.put("mytype", "'post'");
				this.put("datatype", "'json'");
				this.put("jsonReader", "{repeatitems:false,id:'id'}");
				this.put("colModel", "[]");
				this.put("autowidth", "true");
				this.put("height", "'auto'");
				this.put("cmTemplate", "{sortable:false}");
				this.put("multiselect", "true");
				this.put("multikey", "'id'");
				this.put("rowNum", "10");
				this.put("pager", "'#gridPager-"+key+"'");
				this.put("viewrecords", "true");
				this.put("beforeRequest", "function(){" +
						"var data={};" +
						"data['gridKey']='"+key+"';" +
						"data=myjqgrid.buildSearchData('"+getPostParamInputs()+"',data);" +
						"$('#grid-"+key+"').jqGrid('setGridParam',{" +
						"postData:data"+
						"})" +
						"}");
			}
		};
		
		Iterator<String> it=map.keySet().iterator();
		while(it.hasNext()){
			String key=it.next();
			if(options.get(key)==null){
				options.set(key, map.get(key));
			}
		}
		
		return options;
	}
	/**
	 * 初始化此类时，向configMap放入若干参数及值<br>
	 * 改类的附属面板可以通过这个方法获取configMap的参数值<br>
	 * 注意，当要获取的参数没有值时给予默认值
	 * @return
	 */
	public String getConfigByKey(String key){
		if(configMap!=null&&this.configMap.containsKey(key)){
			return this.configMap.get(key);
		}else{
			return "默认值";
		}
	}
	/**
	 *获取查找输入框的selector
	 * @return
	 */
	public String getPostParamInputs(){
		return null;
	}
	/**
	 * 向jqGridMap注册自己
	 * */
	private void registerSelf(){
		HttpSession session=HttpServletUtil.getHttpSession();
		Map<String,JqGridPanel> map=(Map<String, JqGridPanel>) session.getAttribute(ComponentConstant.JQGRID_PANEL_MAP_IN_SESSION);
		if(map==null){
			map=new HashMap<String,JqGridPanel>();
		}
		map.put(key, this);
		
		session.setAttribute(ComponentConstant.JQGRID_PANEL_MAP_IN_SESSION, map);
	}
	public JqGridService getService() {
		return service;
	}
	public void setService(JqGridService service) {
		this.service = service;
	}
	public String getDetailsId() {
		return detailsId;
	}
	public void setDetailsId(String detailsId) {
		this.detailsId = detailsId;
	}
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public DelButton getDelBtn() {
		return delBtn;
	}
	public void setDelBtn(DelButton delBtn) {
		this.delBtn = delBtn;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public ModalWindow getWin() {
		return win;
	}
	public void setWin(ModalWindow win) {
		this.win = win;
	}
	public ModalWindow getWin2() {
		return win2;
	}
	public void setWin2(ModalWindow win2) {
		this.win2 = win2;
	}

}
